﻿Imports System.Guid
Public Class Consultation_CTRL
    Implements IDisposable

    Private _agenda As Agendas
    Private _EDM As AniFormEntities
    Private _consultation As Consultations
    Private _index As Integer = 0
    Private _selectedTypeActe As String
    Private _listeLigne As List(Of LignesConsultations)


#Region "Cycle de vie du controleur CONSULTATION"

    Public Sub New(ByVal EDM As AniFormEntities, ByVal agenda As Agendas)

        _EDM = EDM
        _agenda = agenda
        _consultation = getConsultationCourante()


    End Sub

    Private disposedValue As Boolean = False        ' Pour détecter les appels redondants

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
            End If
            _index = Nothing
            _EDM.Dispose()
            _agenda = Nothing
            For Each element In _listeLigne
                element = Nothing
            Next
            _consultation = Nothing
            _listeLigne.Clear()
            _listeLigne = Nothing

        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region

    Public ReadOnly Property animalCourant() As Animaux
        Get
            Return _agenda.Animaux
        End Get
    End Property

    Public ReadOnly Property veterinaireCourant() As Veterinaires
        Get
            Return Main_CTRL.getInstance.vetoIdentifie
        End Get
    End Property

    Public ReadOnly Property consultationCourante() As Consultations
        Get
            Return _consultation
        End Get
    End Property

    Public ReadOnly Property lignesConsultationCourante() As List(Of LignesConsultations)
        Get
            If _consultation IsNot Nothing Then
                Return _listeLigne
            Else
                Return Nothing
            End If

        End Get
    End Property


    Public Property selectedBaremes() As String
        Get
            Return _selectedTypeActe
        End Get
        Set(ByVal value As String)
            _selectedTypeActe = value
        End Set
    End Property

    Public Property index() As Integer
        Get
            Return _index
        End Get
        Set(ByVal value As Integer)
            _index = value
        End Set
    End Property

    Public ReadOnly Property totalConsultation() As Decimal
        Get
            If lignesConsultationCourante IsNot Nothing Then
                Dim total As Decimal = 0
                For Each ligne In lignesConsultationCourante
                    total += ligne.Prix
                Next
                Return total
            Else
                Return 0
            End If

        End Get
    End Property

    ''' <summary>
    ''' Propriete retournant la liste de Type d'actes (string)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property listeTypeActe() As IEnumerable
        Get
            Dim requete As IEnumerable = From acte In _EDM.Baremes Select acte.TypeActe Distinct
            Return requete
        End Get
    End Property

    ''' <summary>
    ''' Propriete retournant la liste des libelles de type d'actes (string)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ListeLibelle() As IEnumerable
        Get
            Dim requete As IEnumerable = (From unLibelle In _EDM.Baremes Select unLibelle Where unLibelle.TypeActe = _selectedTypeActe)
            Return requete
        End Get
    End Property

    Private Function getConsultationCourante() As Consultations

        Dim laConsultation As Consultations

        Dim requete As IEnumerable = (From uneConsultation In _EDM.Consultations Select uneConsultation Where uneConsultation.DateConsultation = _agenda.DateRdv And uneConsultation.Animaux.CodeAnimal = _agenda.Animaux.CodeAnimal And uneConsultation.Veterinaires.CodeVeto = _agenda.CodeVeto And uneConsultation.Archive = False).AsEnumerable

        laConsultation = requete(0)

        If laConsultation Is Nothing Then
            laConsultation = Consultations.CreateConsultations(NewGuid, _agenda.DateRdv, 0, False)
            laConsultation.Animaux = _agenda.Animaux
            laConsultation.Veterinaires = veterinaireCourant
            laConsultation.Factures = Nothing
            _EDM.SaveChanges(True)
            _listeLigne = New List(Of LignesConsultations)
        Else
            _listeLigne = laConsultation.mesLignes
        End If

        Return laConsultation

    End Function

    Public Sub sauverConsultation(ByVal acte As Baremes, ByVal commentaire As String, ByVal prix As Decimal, ByVal tatouage As String)



        _consultation.Commentaire = commentaire

        Dim ligneAajouter As New LignesConsultations

        ligneAajouter.Archive = False
        If acte.TypeActe = "TATO" Then
            _consultation.Animaux.Tatouage = tatouage
        End If
        ligneAajouter.Baremes = acte
        ligneAajouter.Consultations = _consultation
        ligneAajouter.Prix = prix
        ligneAajouter.RappelEnvoye = False
        ligneAajouter.NumLigne = NewGuid()

        'Pour chaque ligne utilisant un vaccin on enleve un vaccin ds le stock vaccin
        If ligneAajouter.TypeActe = "VACC" Then
            ligneAajouter.Vaccin.QuantiteStock -= 1
        End If


        _listeLigne.Add(ligneAajouter)
        _EDM.AddToLignesConsultations(ligneAajouter)

        _EDM.SaveChanges(True)

        index = _listeLigne.Count

    End Sub

    Public Sub supprimerLigne(ByVal ligne As LignesConsultations)

        Dim ligneAsupprimer As LignesConsultations = (From uneLigne In _EDM.LignesConsultations Where uneLigne.NumLigne = ligne.NumLigne).First

        If ligneAsupprimer.TypeActe = "VACC" Then
            ligneAsupprimer.Vaccin.QuantiteStock += 1
        End If

        _EDM.DeleteObject(ligneAsupprimer)
        _listeLigne.Remove(ligneAsupprimer)

        _EDM.SaveChanges(True)

        _index = 0

    End Sub

    Public Sub supprimerConsultation()

        For Each ligne In _listeLigne

            If ligne.TypeActe = "VACC" Then
                ligne.Vaccin.QuantiteStock += 1
            End If
            _EDM.DeleteObject(ligne)

        Next

        _EDM.SaveChanges(True)

        _EDM.DeleteObject(_consultation)

        _EDM.SaveChanges(True)

        _listeLigne.Clear()

        _listeLigne = Nothing

        _selectedTypeActe = Nothing

        _consultation = getConsultationCourante()

        _index = 0

    End Sub

    Public Sub validerConsultation()

        _consultation.Etat = 1

        _EDM.SaveChanges(True)

    End Sub

    Public Sub modifierConsultation(ByVal commentaire As String)

        _consultation.Commentaire = commentaire

        _EDM.SaveChanges(True)

    End Sub


End Class
